from django.shortcuts import render
from timeholder import models
from timeholder.models import Task
from datetime import datetime
from django.views.decorators.csrf import csrf_exempt
from django.http import JsonResponse

def test(request):
    return render(request,'test.html',{'list':[1,2,4,5]})

def clock(request):

    countdown =1500
    context = {
        'countdown': countdown
    }
    return render(request, 'clock.html', context)
from timeholder import models
from datetime import  datetime,timedelta

@csrf_exempt
def data(request):
    session_date=None
    try:
        if('cur_date' in request.session.keys()):
            session_date=request.session.get('cur_date')
    except Exception:
        pass
    if not session_date:
        session_date = datetime.strftime(datetime.now(), "%Y-%m-%d")
    cur_date = datetime.strptime(session_date,"%Y-%m-%d").date()
    # 用于调试的临时数据
    cur_user = request.user_obj.username
    # 获取该用户的所有活动
    task_obj=models.Task.objects.filter(username=cur_user)
#获取该用户的所有每日总结
    dateDesc_obj=models.DateDesc.objects.filter(username=cur_user).filter(date=cur_date)
    DayDes="今天没有总结哦"
    #获取该用的在cur_date下的每日总结
    for date_desc in dateDesc_obj:
        print(date_desc.date)
        print(cur_date)
        if(date_desc.date==cur_date):
            DayDes=date_desc.des
            break
# 获取该用户的当天活动
    cur_date_task_names,cur_date_task_times=[],[]
    for i in range(len(task_obj)):
        if  task_obj[i].start_datetime.date()==cur_date:
            cur_date_task_names.append(task_obj[i].name)
            cur_date_task_times.append(25)
    totalName=""
#为了正确传递参数，将活动名封装为字符串
    if(len(cur_date_task_names)!=0):
        totalName=cur_date_task_names[0]
    for i in range(len(cur_date_task_names)-1):
        totalName+=","
        totalName+=cur_date_task_names[i+1]
#统计从当天开始，向前推七天，每天的学习时间：
    weekTime=[]
    for i in range(7):
        this_date=cur_date-timedelta(days=i)
        weekTime.append(len([task for task in task_obj if task.start_datetime.date()==this_date])*25)
    weekTime=list(reversed(weekTime))
#统计实现
    return render(request,'data.html',{"dayDes":DayDes,'day24TasksName':totalName,'day24TasksTime':cur_date_task_times,'weekTime':weekTime})
@csrf_exempt
def updateDate(request):
    print("update Date do")
    if request.method == "POST":
        newDate=request.POST.dict().get('selectedData')
        request.session['cur_date'] = datetime.strftime(datetime.strptime(newDate, "%Y-%m-%d"), "%Y-%m-%d")
        return JsonResponse({'status': True,'newDate':newDate})
    return JsonResponse({'status':False})

@csrf_exempt
def updateSummary(request):
    # 更新数据库：
    print("dome")
    cur_user = request.user_obj.username
    cur_date = datetime.strptime(request.session.get('cur_date'),"%Y-%m-%d").date()
    new_summary=request.POST.dict()['newSummary']
    dateDesc_obj, _ = models.DateDesc.objects.get_or_create(username=request.user_obj.username, date=cur_date)
    print(new_summary)
    dateDesc_obj.des=new_summary
    dateDesc_obj.save()
    return JsonResponse({'status':True})
